﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
using System.IO;
using System.Threading.Tasks;

namespace TianYu.Core.Pdf
{
    public class CommonHelper
    {
        public static BaseColor GetBaseColor(int index)
        {
            BaseColor baseColor;
            switch (index)
            {
                case 1:
                    baseColor = BaseColor.WHITE;
                    break;
                case 2:
                    baseColor = BaseColor.BLACK;
                    break;
                case 3:
                    baseColor = BaseColor.BLUE;
                    break;
                default:
                    baseColor = BaseColor.WHITE;
                    break;
            }

            return baseColor;
        }

        /// <summary>
        /// 默认行距
        /// </summary>
        public static readonly float BaseLeading = 9;

        /// <summary>
        /// 线条缩小倍数
        /// </summary>
        public static readonly float LineWidthReduce = 1.5f;

        public static BaseFont GetFontFamilyPath(string fontFamily)
        {
            string fileName;
            switch (fontFamily.ToLower())
            {
                case "黑体":
                    fileName = "simhei.ttf";
                    break;
                case "arial":
                    fileName = "arial.ttf";
                    break;
                case "宋体":
                    fileName = "simsun.ttc,0";
                    break;
                case "tahoma":
                    fileName = "tahoma.ttf";
                    break;
                case "verdana":
                    fileName = "verdana.ttf";
                    break;
                case "helvetica"://本地没有，暂默认
                    fileName = "arial.ttf";
                    break;
                case "lucida grande"://本地没有，暂默认
                    fileName = "arial.ttf";
                    break;
                case "times new roman":
                    fileName = "times.ttf";
                    break;
                case "georgia":
                    fileName = "gabriola.ttf";
                    break;
                case "sans-serif"://本地没有，暂默认
                    fileName = "arial.ttf";
                    break;
                case "ms ui gothic"://本地没有，暂默认
                    fileName = "msgothic.ttc,0";
                    break;
                default:
                    fileName = "arial.ttf";
                    break;
            }

            return BaseFont.CreateFont($"C:\\Windows\\Fonts\\{fileName}", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        }

        public static LineStyle GetLineStyle(int style)
        {
            LineStyle lineStyle;
            switch (style)
            {
                case 1:
                    lineStyle = LineStyle.Solid;//实线
                    break;
                case 2:
                    lineStyle = LineStyle.Dotted;//点状线
                    break;
                case 3:
                    lineStyle = LineStyle.Dashed;//虚线
                    break;
                default:
                    lineStyle = LineStyle.Solid;//实线
                    break;
            }

            return lineStyle;
        }

        public static async Task<byte[]> GetTablePDFByte(DataTable data, string title, List<float> widths)
        {
            using MemoryStream stream = new();
            Document document = new(PageSize.A4, 5, 5, 50, 5);
            try
            {
                PdfWriter writer = PdfWriter.GetInstance(document, stream);
                document.Open();

                //设置字体,支持中文
                BaseFont bfChinese = GetFontFamilyPath("宋体");
                Font titleFont = new(bfChinese, 14, Font.NORMAL, new BaseColor(0, 0, 0));
                Font dataFont = new(bfChinese, 10, Font.NORMAL, new BaseColor(0, 0, 0));

                Phrase phrase = new(title, titleFont);
                ColumnText.ShowTextAligned(writer.DirectContent, 0, phrase, 220, 810, 0);

                PdfPTable table = new(data.Columns.Count + 1);
                table.TotalWidth = 560;
                table.LockedWidth = true;
                table.DefaultCell.Padding = 5;
                table.DefaultCell.BorderWidth = 1;
                table.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                table.DefaultCell.VerticalAlignment = iTextSharp.text.Element.ALIGN_CENTER;

                widths.Insert(0, 80);
                table.SetWidths(widths.ToArray());
                table.AddCell(new Phrase("序号", dataFont));

                //将datatable表头转换成PDFTable的表头
                foreach (DataColumn dc in data.Columns)
                {
                    table.AddCell(new Phrase(dc.ColumnName, dataFont));
                }

                //插入数据
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    table.AddCell(new Phrase((i + 1).ToString(), dataFont));
                    for (int j = 0; j < data.Columns.Count; j++)
                    {
                        table.AddCell(new Phrase(data.Rows[i][j].ToString(), dataFont));
                    }
                }
                document.Add(table);
                document.Close();
                writer.Close();
            }
            catch
            {
                throw;
            }
            finally
            {
                if (document.IsOpen())
                {
                    document.Close();
                }
            }

            return await Task.FromResult(stream.ToArray());
        }

        public static int GetFontStyle(int style)
        {
            // 设置字体，如果不设置添加的中文将无法显示
            var fontStyle = iTextSharp.text.Font.NORMAL;

            switch (style)
            {
                case 0://正常
                    fontStyle = iTextSharp.text.Font.NORMAL;
                    break;
                case 1://加粗
                    fontStyle = iTextSharp.text.Font.BOLD;
                    break;
                case 2://斜体
                    fontStyle = iTextSharp.text.Font.ITALIC;
                    break;
                case 4://下划线
                    fontStyle = iTextSharp.text.Font.UNDERLINE;
                    break;
                case 5://加粗+下划线
                    fontStyle = iTextSharp.text.Font.BOLD | iTextSharp.text.Font.UNDERLINE;
                    break;
                case 8:
                    fontStyle = iTextSharp.text.Font.STRIKETHRU;
                    break;
            }

            return fontStyle;
        }

        public static int GetAlignment(string alignStr)
        {
            var align = PdfContentByte.ALIGN_LEFT;
            switch (alignStr.ToLower())
            {
                case "left":
                    align = PdfContentByte.ALIGN_LEFT;
                    break;
                case "center":
                    align = PdfContentByte.ALIGN_CENTER;
                    break;
                case "right":
                    align = PdfContentByte.ALIGN_RIGHT;
                    break;
            }
            return align;
        }
    }
}